import axios from "axios";
import {showFailToast} from "vant";

const myAxios = axios.create({
    baseURL: "/api"
})

// 添加请求拦截器
myAxios.interceptors.request.use(function (config) {

    // 获取token
    const token = localStorage.getItem("token")

    // 判断是否存在token
    if (token == null || token == undefined) {
        // 不存在token
        return config
    }

    // 添加token至请求拦截器
    config.headers.set("Authorization",`Bearer ${token}`)

    return config
})

// 添加响应拦截器
myAxios.interceptors.response.use(function (response) {
    // console.log(response)
    return response
},function (error){
    showFailToast(error.response.data.msg);
    return Promise.reject(error);
})

export default myAxios